package com.hotel.BL;

import java.util.ArrayList;
import java.util.List;

import com.EntityName;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.api.datastore.FetchOptions;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.appengine.api.datastore.Query.CompositeFilterOperator;
import com.google.appengine.api.datastore.Query.Filter;
import com.google.appengine.api.datastore.Query.FilterOperator;
import com.google.appengine.api.datastore.Query.FilterPredicate;

import data.Salutation;

public class SalutationLogic extends AbstractBL {
	public List<Salutation> GenerateSalutation(long hotelId)
	{
		List<Salutation> list = new ArrayList<Salutation>();
		
		Salutation salutation;
		if(GetSalutation(1,"Mr.") == null){
			salutation = new Salutation();
			salutation.setHotelId(hotelId);
			salutation.setTitle("Mr.");
			list.add(salutation);
		}
		if(GetSalutation(1,"Ms.") == null){
			salutation = new Salutation();
			salutation.setHotelId(hotelId);
			salutation.setTitle("Ms.");
			list.add(salutation);
		}
		if(GetSalutation(1,"Mrs.") == null){
			salutation = new Salutation();
			salutation.setHotelId(hotelId);
			salutation.setTitle("Mrs.");
			list.add(salutation);
		}
		
		return list;
	}
	
	public Salutation GetSalutation(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Salutation, id);
		try {
			Entity lang = datastore.get(key);
			Salutation c = new Salutation();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public Salutation GetSalutationById(long id)
	{
		Key key = KeyFactory.createKey(EntityName.Salutation, id);
		try {
			Entity lang = datastore.get(key);
			Salutation c = new Salutation();
			c.setEntity(lang);
			return c;
		} catch (EntityNotFoundException e) {
			return null;
		}
	}
	
	public Salutation GetSalutation(long hotelId,String title)
	{
		Query q = new Query(EntityName.Salutation);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);
		Filter filterTitle = new FilterPredicate("title",FilterOperator.EQUAL,title);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable,filterTitle);
		
		q.setFilter(filter);
		
		return GetSalutation(datastore.prepare(q).asSingleEntity());
	}
	
	public List<Salutation> GetAllSalutation(long hotelId)
	{
		Query q = new Query(EntityName.Salutation);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterStatus = new FilterPredicate("status",FilterOperator.EQUAL,true);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterStatus, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
	
	public Salutation GetSalutation(Entity entity)
	{
		if(entity == null) return null;
		Salutation item = new Salutation();
		item.setEntity(entity);
		return item;
	}
	
	public List<Salutation> GetList(List<Entity> entities)
	{
		List<Salutation> list = new ArrayList<Salutation>();
		for(Entity e : entities)
		{
			Salutation t = new Salutation();
			t.setEntity(e);
			list.add(t);
		}
		return list;
	}

	public Salutation AddSalutation(long hotelId,String title) throws EntityNotFoundException{
		Salutation t = new Salutation();
		t.setHotelId(hotelId);
		t.setTitle(title);
		t.setAvaliable(true);
		t.setStatus(true);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);
		return t;
	}
	
	public Salutation EditSalutation(long salutationId,String title) throws EntityNotFoundException{
		Salutation t = GetSalutation(salutationId);
		t.setTitle(title);

		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public Salutation DeleteSalutation(long salutationId) throws EntityNotFoundException{
		Salutation t = GetSalutation(salutationId);
		t.setAvaliable(false);
		t.setStatus(false);
		
		Key k = datastore.put(t.getEntity());
		t.setId(k.getId());
		t.setKey(k);

		return t;
	}
	
	public Salutation ChangeStatus(long salutationId) throws EntityNotFoundException{
		
		Salutation a = GetSalutation(salutationId);
		if(a.isStatus() == true){
			a.setStatus(false);
		}else if(a.isStatus() == false){
			a.setStatus(true);
		}
		 
		Key k = datastore.put(a.getEntity());
		a.setId(k.getId());
		a.setKey(k);

		return a;
	}
	
	public List<Salutation> GetAllSalutation4AdminLimit(long hotelId,long offset,long line)
	{
		Query q = new Query(EntityName.Salutation);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withLimit((int) line).offset((int)offset)));
	}
	
	
	public List<Salutation> GetAllSalutation4Admin(long hotelId)
	{
		Query q = new Query(EntityName.Salutation);
		
		Filter filterHotel = new FilterPredicate("hotelId",FilterOperator.EQUAL,hotelId);
		Filter filterAvaliable = new FilterPredicate("avaliable",FilterOperator.EQUAL,true);

		Filter filter = CompositeFilterOperator.and(filterHotel, filterAvaliable);
		
		q.setFilter(filter);
		
		return GetList(datastore.prepare(q).asList(FetchOptions.Builder.withDefaults()));
	}
}
